package edu.hawaii.solardecathlon.page.electricity;

import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

/**
 * Extends ListView for the Electricity page. The ElectricityModalWindow is used here.
 * 
 * @author Bret Ikehara
 */
public class ElectricityListView extends ListView<Electricity> {

  /**
   * Serial ID.
   */
  private static final long serialVersionUID = 9089823736171587625L;
  private final ElectricityModalWindow modal;

  /**
   * Constructor for creating the list of appliances.
   * 
   * @param id String
   * @param list List<? extends Electricity>
   * @param modal final ElectricityModalWindow
   */
  public ElectricityListView(String id, List<? extends Electricity> list,
      final ElectricityModalWindow modal) {
    super(id, list);
    this.modal = modal;
  }

  /**
   * Populates the Electricity details page.
   * 
   * @param item ListItem<Electricity>
   */
  @Override
  protected void populateItem(ListItem<Electricity> item) {
    final Electricity elec = item.getModelObject();
    
    AjaxLink<String> link = new AjaxLink<String>("ElecLimit") {

      /**
       * Serial ID.
       */
      private static final long serialVersionUID = -6429739023291996997L;

      /**
       * Open the dialog box.
       * 
       * @param targe AjaxRequestTarget
       */
      @Override
      public void onClick(AjaxRequestTarget target) {
        modal.setElec(elec);
        modal.show(target);
      }
    };
    link.add(new Label("ElecLimitLabel", String.valueOf(elec.getLimit())));

    item.add(new Label("ElecApp", elec.getAppliance()));
    item.add(new Label("ElecUsage", String.valueOf(elec.getUsage())));
    item.add(new Label("ElecConsump", String.valueOf(elec.getUsage())));
    item.add(link);
  }
}
